//
//  AppDelegate+Jpush.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/5/6.
//

import UIKit

#if DEVELOPER
    var jpushAppKey = "29b601f58272fe064ff88078"
    var production = false
#else
    var jpushAppKey = "c1eb6031454280e17d27a613"
    var production = true
#endif

extension AppDelegate {
    func setupJpush(_ launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {

        // JPush初始化SDK
        let entity = JPUSHRegisterEntity()
        entity.types = 1 << 0 | 1 << 1 | 1 << 2
        JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)

        JPUSHService.setup(withOption: launchOptions,
                           appKey: jpushAppKey,
                           channel: "App Store",
                           apsForProduction: production,
                           advertisingIdentifier: nil)
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // 注册设备
        JPUSHService.registerDeviceToken(deviceToken)
        JPUSHService.registrationIDCompletionHandler { code, registrationID in
            self.handlerJpushRegistrationId(registrationID: registrationID)
        }
    }

    static func resetBadge() {
        JPUSHService.resetBadge()
        UIApplication.shared.applicationIconBadgeNumber = 0
    }
    
}

extension AppDelegate: JPUSHRegisterDelegate {
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {

    }

    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {

    }

    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, openSettingsFor notification: UNNotification!) {

    }

    func jpushNotificationAuthorization(_ status: JPAuthorizationStatus, withInfo info: [AnyHashable : Any]!) {

    }
}

extension AppDelegate {
    private func handlerJpushRegistrationId(registrationID: String?) {
        guard let regId = registrationID else { return }
        UserDefaults.jpushRegistrationId = regId
        SEAuthViewModel.bindJPushRegistrationID()
    }
}

